package com.nameli.admin.config.exception;

import com.nameli.SIM.util.ReturnStatus;
import com.nameli.SIM.util.response.JsonResult;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.*;
import org.apache.shiro.authc.pam.UnsupportedTokenException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.security.auth.message.AuthException;

/**
 * @author NAME-L
 * @Description TODO
 * @className MyExceptionHandler
 * @date 2022-01-07 14:56:05
 */
@RestControllerAdvice
public class MyExceptionHandler {

    @ExceptionHandler(value = AuthenticationException.class)
    public JsonResult handleCustomException(AuthenticationException e){
        JsonResult jsonResult = null;
        String errorCode = "";//返回状态码
        String errorMsg = "";//返回的提示信息
        if (e instanceof IncorrectCredentialsException){
            errorCode = ReturnStatus.NO_LOGIN;
            errorMsg = "登录已过期，请重新登录";
            jsonResult = JsonResult.withoutData(errorCode, errorMsg);
        }else if (e instanceof UnsupportedTokenException){
            errorCode = ReturnStatus.ERROR;
            errorMsg = "身份令牌异常，不支持的身份令牌";
            jsonResult = JsonResult.withoutData(errorCode, errorMsg);
        }else if (e instanceof UnknownAccountException){
            errorCode = ReturnStatus.NO_LOGIN;
            errorMsg = "账号不存在，请重新输入";
            jsonResult = JsonResult.withoutData(errorCode, errorMsg);
        }else if (e instanceof LockedAccountException){
            errorCode = ReturnStatus.NO_LOGIN;
            errorMsg = "账号被锁定，请联系管理员";
            jsonResult = JsonResult.withoutData(errorCode, errorMsg);
        }else if (e instanceof DisabledAccountException){
            errorCode = ReturnStatus.NO_LOGIN;
            errorMsg = "账号被锁定，请联系管理员";
            jsonResult = JsonResult.withoutData(errorCode, errorMsg);
        }
        return jsonResult;
    }
}
